<?php /* -*- mode: php; coding: utf-8 -*- */

/* Todo: move the archive/ file to display several entries, the count
   displayed in the URL--can we display the count in the URL? I don't think
   we want to. No, we should be able to access the archive by month, with
   some maximum page size determining the number of entries we show before
   moving to another archive/ page, with a date in its URI. We have an
   entry/ file to display individual entries. */

/* Load up the functions, and initialise the connection to the database. */

require 'require/blog-functions.phtml';

/* Deconstruct the URI, to allow us to find work out the date for which
   the archive is to be constructed. */

$expl = explode('/', $_SERVER['REQUEST_URI']);

/* This page is specific to the archive; it's no use calling it as something
   else. It shouldn't ever get called as something else, though, which is why
   this is an assertion, rather than an actual check. */
assert($expl[1] == 'archive' || $expl[1] == 'archive.php');

/* Work out which style of archive we're using; day, month, year. */
$style = $expl[2]; 

$entries_to_display = null;

if ($style == 'month') {
	/* '/month/2003/september' . If 2003 isn't mentioned, default
	   to the current year, if we're after the mentioned month; otherwise,
	   last year. 

	   If the month isn't given, assume the current month. However, in
	   that case, we would be better served redirecting them to the index
	   page, as that is better suited to displaying the current month.

           For earlier entries in the same month, the URL format
           /month/2003/september/earlier-than/123456789 should be used.

	   For later entries, if there are any, the format 
           /month/2003/september/later-than/123456789 should be used. */

	$title_summary = ucfirst($expl[4].' '.$expl[3]);

	if (isset($expl[6])) {
		if ('earlier-than' == $expl[5]) {
			/* We're looking for entries earlier than this
			   timestamp, in this month. */
			$latest_timestamp = $expl[6] + 1; 
			
		} else {
			assert ('later-than' == $expl[5]);
			
		}
	} else {
		$late_month_timestamp = strtotime('27 '.$expl[4].' '.
						  $expl[3]); 
		$next_month_first_timestamp = strtotime(
			strftime('1 %B %Y', $late_month_timestamp + 1296000));
		$latest_timestamp = $next_month_first_timestamp - 10;
	}

	/* Okay, let's think out the full algorithm for this. I judge that
	   the amount of entries we want to show on every page will be
	   static, so for these archive pages, we will use the
	   PAGE_ENTRIES_CONTENT_GUIDE, and have directional arrows pointing
	   to the previous entry and next entries in the series.

	   The direction arrows should point us towards a URL that limits
	   the results to be seen; for the monthly page, the earliest should
	   be the last second of the previous month. The latest should be
	   the last second of this month for the first displayed monthly
	   page, and the timestamp of the earliest already displayed entry,
	   minus one, for further displayed monthly pages for the same
	   month. */

	$entries_to_display = create_entries_to_display ($latest_timestamp,
					 PAGE_ENTRIES_CONTENT_GUIDE, 
						 THIS_MONTH_ONLY); 
	if (count($entries_to_display)) {
		/* What's the earliest timestamp of this month? */
		$first_of_month_timestamp = strtotime('1 '.$expl[4].' '.
						   $expl[3]);

		/* What's the entry before the earliest one on this page? */
		$previous_link_timestamp = 
			entry_before($entries_to_display
				     [count($entries_to_display) - 1 ], true);
		$previous_link = '';
		if ($previous_link_timestamp) {
			if ($previous_link_timestamp < 
			    $first_of_month_timestamp) {
				/* No entries earlier than the ones
				   displayed on this page for this month;
				   send them to the previous month. */
				$href = strtolower(strftime(
					   '/archive/month/%Y/%B/',
					   $previous_link_timestamp));
				$previous_link = 
					strftime('<a title="View entries for '.
						 '%B" class="nav_link" '.
						 'href="'.$href.'">',
						 $previous_link_timestamp);
				$previous_link .= 
					strtoupper(strftime('%B, %Y &#8594; ',
					    $previous_link_timestamp)).' </a>';
			} else {
				/* Send them to earlier in this month. */
				$href = strtolower(strftime(
					'/archive/month/%Y/%B/earlier-than/'.
					($previous_link_timestamp - 1),
					$previous_link_timestamp - 1));
				$previous_link =
					strftime('<a title="View earlier '.
					 'entries for %B" class="nav_link" '.
						 'href="'.$href.'">',
						 $previous_link_timestamp);
				$previous_link .= 'Earlier in '.
					strtoupper(
					strftime('%B, %Y &#8594;'.
					 '</a>', $previous_link_timestamp));
			}
		}

		$next_link_timestamp = entry_after($entries_to_display[0]);
		$next_link = '';
		if ($next_link_timestamp) {
			$href = strtolower(strftime("/archive/month/%Y/%B/",
						    $next_link_timestamp));
			$next_link = strftime('<a title="View entries for '.
					      '%B" class="nav_link" href="'.
					      $href, $next_link_timestamp);
			$next_link .= strtoupper(strftime('">&#8592; %B, %Y ',
						  $next_link_timestamp));
			$next_link .= '</a>';
		}
	} else {
		/* No entries are appropriate for this month; forward them
		   to a page that will guarantee them entries. */
		$ts = entry_before($latest_timestamp, true);
		header('Location: /archive/month/'.
		       strtolower(strftime('%Y/%B/', $ts)));
		exit;
	}
	
} else if ($style == 'day') {
	$title_summary = ucfirst($expl[4].' '.$expl[5]);

	$day_start_timestamp = strtotime($expl[5].' '.$expl[4].' '.$expl[3]); 

	/* (- (* 60 60 24) 1) */
	$latest_timestamp = $day_start_timestamp + 86399; 

	/* Take entries for today. */
	$entries_to_display = create_entries_to_display ($latest_timestamp,
					 PAGE_ENTRIES_CONTENT_GUIDE, 
						 THIS_DAY_ONLY); 

	if (count($entries_to_display)) {
		$previous_link_timestamp = 
			entry_before($entries_to_display
				     [count($entries_to_display) - 1], true);
		$previous_link = '';
		if ($previous_link_timestamp) {
			$previous_link = strftime('<a title="View entry for '.
			  '%A" href="/archive/day/%Y/%B/%d">'.
						  '%A, %e &#8594; </a>',
						  $previous_link_timestamp);

		}

		$next_link_timestamp = entry_after($entries_to_display[0]);
		$next_link = '';
		if ($next_link_timestamp) {
			$next_link = strftime('<a title="View entry for '.
				  '%A" href="/archive/day/%Y/%B/%d">'.
						  '&#8592; %A, %e </a>',
						  $next_link_timestamp);
		}
	}
} else assert('false && "You must specify a style."');

/* Okay, 

*/

print_prologue($title_summary.' archive — Aidan Kehoe’s Weblog ');

display_side_bar(); 

?>
    <div class="realbody">
<?php

print_banner($next_link, $previous_link);

foreach ($entries_to_display as $latest_entry_timestamp) {
	$hour = date('G', $latest_entry_timestamp); 
	$meridiem = $hour > 12 ? 'POST&#183;MERIDIEM' : 
	'ANTE&#183;MERIDIEM';

      ?>
      <div class="log_body">
	<p style="text-align: right">
	  <span class="entry_summary"> <?php 
	echo htmlspecialchars(get_entry_summary($latest_entry_timestamp), 
				     ENT_NOQUOTES); ?>
	  </span>
	  <a title="Link directly to this entry." href="<?php 
		echo '/entry/'.date('Y/m/d/H:i/',
			$latest_entry_timestamp);?>" name="<?php
	echo $latest_entry_timestamp; ?>">
	    <?php echo date('jS \o\f F, Y', $latest_entry_timestamp).
		    ' '.$meridiem.
		    ' '.date('h:i', $latest_entry_timestamp); ?>
	  </a>
	</p>
	<?php display_markedup($latest_entry_timestamp); ?>
      </div>
      <?php display_last_comment($latest_entry_timestamp); ?>
      <?php
} /* End of the foreach. */
      ?>
    </div>
<?php

print_epilogue();

 ?>